package com.xiaoyg.receive;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author xiaoyg
 * @Title: ReceiveLogsTopicForXiaoyg
 * @ProjectName rabbitmq-topic
 * @Description: TODO
 * @date 2018/12/419:04
 */
public class ReceiveLogsTopicForXiaoyg {

    private static final String EXCHANGE_NAME = "ex_topic_logs";

    public static void main(String[] args) throws IOException, InterruptedException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.8.129");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setPort(AMQP.PROTOCOL.PORT);

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"topic");

        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName,EXCHANGE_NAME,"xiaoyg.*");

        System.out.println(" [*] waiting for messages about xiaoyg. to exit press ctrl +c");

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName,true,consumer);

        while(true){
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            String routingKey = delivery.getEnvelope().getRoutingKey();

            System.out.println(" [x] Received routingKey = " + routingKey
                    + ",msg = " + message + ".");
        }

    }

}
